local skynet = require 'skynet'
require 'skynet.manager'

require 'nggs.base.const'
local log = require 'nggs.base.log'
local nmath = require 'nggs.base.math'
local inspect = require 'nggs.base.inspect'
local class = require 'nggs.base.class'

skynet.error('test5 started')

local base_chapter = {
    {
        chapter = 1,
        name = '大丘丘',
        id = 101,
        battleid = 2,
        guanka = 1,
        reward = {
            {1001, 10},
            {1002, 10}
        },
        rewardarmy = {
            {101, 10}
        }
    },
    {
        chapter = 1,
        name = '大丘丘',
        id = 101,
        battleid = 3,
        guanka = 2,
        rewardarmy = {
            {101, 10}
        }
    },
    {
        chapter = 1,
        name = '大丘丘',
        id = 101,
        battleid = 4,
        guanka = 3,
        reward = {
            {1001, 10},
            {1002, 10}
        },
        rewardarmy = {
            {101, 10}
        }
    },
    {
        chapter = 2,
        name = '二丘丘',
        id = 201,
        battleid = 5,
        guanka = 1,
        rewardarmy = {
            {201, 10}
        }
    },
    {
        chapter = 2,
        name = '二丘丘',
        id = 201,
        battleid = 6,
        guanka = 2,
        reward = {
            {1001, 10},
            {1002, 10}
        },
        rewardarmy = {
            {201, 10}
        }
    },
    {
        chapter = 2,
        name = '二丘丘',
        id = 201,
        battleid = 7,
        guanka = 3,
        rewardarmy = {
            {201, 10}
        }
    }
}

local function resetKey(tbl, tblName, keyName)
    --无需设置key直接返回
    if keyName == '' or #keyName == 0 then
        return tbl
    end

    local newTbl = {}

    if type(keyName) == 'string' then
        for k, v in ipairs(tbl) do
            local key = v[keyName]
            if newTbl[key] then
                assert(false, tblName .. ' duplicate data key name=' .. keyName .. ' key value=' .. key)
            end
            newTbl[key] = v
        end
    elseif type(keyName) == 'table' then
        for _, v in ipairs(tbl) do
            local t = newTbl
            for i = 1, #keyName do
                local k = keyName[i]
                if i < #keyName then
                    if not t[v[k]] then
                        t[v[k]] = {}
                    end
                    t = t[v[k]]
                else
                    if type(k) == 'string' then
                        t[v[k]] = v
                    elseif k == 0 then
                        local id = #t + 1
                        t[id] = v
                        v._static_key = id
                    else
                        assert(nil, tblName .. ' unknown key ' .. k)
                    end
                end
            end
        end
    else
        assert('unsupported key type')
    end

    return newTbl
end

skynet.start(
    function()
        skynet.timeout(
            300,
            function()
                skynet.error('test5 end')
                skynet.sleep(0)
                skynet.abort()
            end
        )

        local t = resetKey(base_chapter, 'base_chapter', {'id', 'battleid'})
        skynet.error(inspect(t))
    end
)
